package com.bocai.service.impl;

import com.bocai.bean.CustomEvent;
import com.bocai.mapper.CustomEventMapper;
import com.bocai.service.ICustomEventService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.DateUtils;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class CustomEventServiceImpl implements ICustomEventService {

    @Autowired
    private CustomEventMapper customEventMapper;

    @Override
    public CustomEvent selectEvent(Integer eventId) {
        return customEventMapper.selectByPrimaryKey(eventId);
    }

    @Override
    public int updateEvent(CustomEvent event) {
        return customEventMapper.updateByPrimaryKeySelective(event);
    }

    @Override
    public int insertEvent(CustomEvent event) {
        return customEventMapper.insertSelective(event);
    }

    @Override
    public ResponseData setEvent(Integer eventId,
                                 Integer isHot,
                                 Integer isEnable,
                                 String ruleTypes,
                                 String homeName,
                                 String awayName,
                                 String eventTime,
                                 ResponseData data) {
        data.setCodeEnum(CodeEnum.CODE_1);

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{eventId, homeName, awayName, eventTime});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }
        CustomEvent event = new CustomEvent();

        event.setEventId(eventId);
        event.setIsHot(isHot);
        event.setIsEnable(isEnable);
        event.setEventHomeName(homeName);
        event.setEventAwayName(awayName);
        event.setRuleTypes(ruleTypes);

        try {
            event.setEventTime(DateUtils.parse(eventTime));
        } catch (ParseException e) {
            e.printStackTrace();
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        if (ObjUtils.isNotEmpty(this.selectEvent(eventId))) {
            if (this.updateEvent(event) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        } else {
            if (this.insertEvent(event) > 0) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            }
        }

        return data;
    }

    @Override
    public ResponseData setEvents(JSONArray events, ResponseData data) {
        for (int i = 0;i < events.size();i ++){
            JSONObject event = events.getJSONObject(i);

            Integer eventId = event.getInt("eventId");
            Integer isHot = null;
            Integer isEnable = null;
            String ruleTypes = null;

            if(event.has("isHot")){
                isHot = event.getInt("isHot");
            }

            if(event.has("isEnable")){
                isEnable = event.getInt("isEnable");
            }

            if(event.has("ruleTypes")){
                ruleTypes = event.getString("ruleTypes");
            }

            String homeName = event.getString("homeName");
            String awayName = event.getString("awayName");
            String eventTime = event.getString("eventTime");

            data = this.setEvent(eventId,isHot,isEnable,ruleTypes,homeName,awayName,eventTime,data);

            if (!data.getCode().equals(CodeEnum.SUCCESS.getCode())){
                return data;
            }
        }
        return data;
    }


    @Override
    public int updateEventStatus(CustomEvent event) {
        return customEventMapper.updateEventStatus(event);
    }

    @Override
    public List<CustomEvent> selectEvents(CustomEvent customEvent) {
        return customEventMapper.selectHotEvents(customEvent);
    }


}
